<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户ID" >
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入最后消息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table   height="550" style="width: 100%" v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">

      <el-table-column label="聊天室id" align="center" prop="chatRoomId" width="300" show-overflow-tooltip />
      <el-table-column label="备注" align="center" prop="remark" width="180" show-overflow-tooltip />
      <el-table-column label="聊天室别名" align="center" prop="chatRoomNickName" width="180" show-overflow-tooltip />
      <el-table-column label="会话类型 " align="center" prop="isGroupChat" >
        <template slot-scope="scope">
          <span v-show="scope.row.isGroupChat==0">单聊</span>
          <span v-show="scope.row.isGroupChat==1">群聊</span>
        </template>
      </el-table-column>
      <el-table-column label="群聊名称" align="center" prop="groupChatName" width="180" show-overflow-tooltip />
      <!-- <el-table-column label="最后消息" align="center" prop="lastMessage" width="180" show-overflow-tooltip /> -->
      <!-- <el-table-column label="消息类型" align="center" prop="lastMessageType" >
        <template slot-scope="scope">
          <span v-show="scope.row.isGroupChat==0">文字消息</span>
          <span v-show="scope.row.isGroupChat==1">语音消息</span>
          <span v-show="scope.row.isGroupChat==2">图片消息</span>
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >详情</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改聊天室对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-table   height="550" style="width: 100%"  :data="liaotiaosArr">

        <el-table-column label="头像地址" align="center" prop="txUrl" width="180" >
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.txPic"
              :preview-src-list="[scope.row.txPic]">
            </el-image>
          </template>
        </el-table-column>

        <el-table-column label="用户昵称" align="center" prop="nickName"  />
        <el-table-column label="用户id" align="center" prop="userId"  />

        <el-table-column label="消息内容" align="center" prop="message" width="180" show-overflow-tooltip />

        <el-table-column label="消息类型 " align="center" prop="messageType" >
          <template slot-scope="scope">
            <span v-show="scope.row.messageType==0">文字消息</span>
            <!--<span v-show="scope.row.messageType==1">语音消息</span>-->
            <span v-show="scope.row.messageType==1">图片消息</span>
            <!--<span v-show="scope.row.messageType==3">地图</span>-->
            <!--<span v-show="scope.row.messageType==4">视频</span>-->
          </template>
        </el-table-column>
       <el-table-column label="消息时间" align="center" prop="createTime" width="180" show-overflow-tooltip />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="open = false">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoom, getRoom, delRoom, addRoom, updateRoom, exportRoom } from "@/api/lanbangkeji/charRoom/room";

export default {
  name: "Room",
  components: {
  },
  data() {
    return {
      //聊天室内容
      liaotiaosArr:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 聊天室表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lastMessage: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询聊天室列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chatRoomId: null,
        createTime: null,
        updateTime: null,
        remark: null,
        chatRoomNickName: null,
        isGroupChat: null,
        groupChatName: null,
        lastMessage: null,
        lastMessageType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chatRoomId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加聊天室";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      var obj = {}
      obj.chatRoomId = row.chatRoomId
      getRoom(obj).then(response => {
        this.liaotiaosArr = response.rows;
        this.open = true;
        this.title = "聊天内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.chatRoomId != null) {
            updateRoom(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoom(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const chatRoomIds = row.chatRoomId || this.ids;
      this.$confirm('是否确认删除聊天室编号为"' + chatRoomIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRoom(chatRoomIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有聊天室数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRoom(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
